package cn.xfdzx.android.apps.shop.presenter;

import cn.xfdzx.android.apps.shop.base.BasePresenter;
import cn.xfdzx.android.apps.shop.bean.BaseObjectBean;
import cn.xfdzx.android.apps.shop.bean.ConfirmGoodBeen;
import cn.xfdzx.android.apps.shop.bean.FamilyBuyBeen;
import cn.xfdzx.android.apps.shop.bean.FoundGoodTitleBeen;
import cn.xfdzx.android.apps.shop.bean.GoodsLimitBean;
import cn.xfdzx.android.apps.shop.bean.GoodsMiaoSBean;
import cn.xfdzx.android.apps.shop.bean.HomeMarketInitDataBeen;
import cn.xfdzx.android.apps.shop.bean.HomeMarketSelectBeen;
import cn.xfdzx.android.apps.shop.bean.SelfOwnedShopBeen;
import cn.xfdzx.android.apps.shop.contract.HomeMarketContract;
import cn.xfdzx.android.apps.shop.model.HomeMarketModel;
import cn.xfdzx.android.apps.shop.net.RxScheduler;
import io.reactivex.functions.Consumer;

/**
 * @author azheng
 * @date 2018/6/4.
 * Description：
 */
public class HomeMarketPresenter extends BasePresenter<HomeMarketContract.View> implements HomeMarketContract.Presenter {

    private HomeMarketContract.Model model;

    public HomeMarketPresenter() {
        model = new HomeMarketModel();
    }


    @Override
    public void initData() {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.initData().compose(RxScheduler.<BaseObjectBean<HomeMarketInitDataBeen>>Flo_io_main())
                .as(mView.<BaseObjectBean<HomeMarketInitDataBeen>>bindAutoDispose()).subscribe(new Consumer<BaseObjectBean<HomeMarketInitDataBeen>>() {
            @Override
            public void accept(BaseObjectBean<HomeMarketInitDataBeen> baseObjectBean) throws Exception {
                mView.hideLoading();
                mView.onInitDataSuccess(baseObjectBean);

            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                mView.hideLoading();
                mView.onError(throwable);

            }
        });


    }
    @Override
    public void initTitle(String pid,String index_status) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }

        model.initTitle(pid,index_status).compose(RxScheduler.<FoundGoodTitleBeen>Flo_io_main())
                .as(mView.<FoundGoodTitleBeen>bindAutoDispose()).subscribe(new Consumer<FoundGoodTitleBeen>() {
            @Override
            public void accept(FoundGoodTitleBeen baseObjectBean) throws Exception {

                mView.onInitTitleSuccess(baseObjectBean);

            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {

                mView.onError(throwable);

            }
        });
    }

    @Override
    public void familyBuy(String sales_model) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }

        model.familyBuy(sales_model).compose(RxScheduler.<FamilyBuyBeen>Flo_io_main())
                .as(mView.<FamilyBuyBeen>bindAutoDispose()).subscribe(new Consumer<FamilyBuyBeen>() {
            @Override
            public void accept(FamilyBuyBeen baseObjectBean) throws Exception {

                mView.onFamilyBuySuccess(baseObjectBean);

            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {

                mView.onError(throwable);

            }
        });
    }

    @Override
    public void addCar(String is_new, String uniqueId, String productId, String cartNum, int position, int total_num) {
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.addCar(is_new,uniqueId,productId,cartNum) .compose(RxScheduler.<ConfirmGoodBeen>Flo_io_main())
                .as(mView.<ConfirmGoodBeen>bindAutoDispose())
                .subscribe(new Consumer<ConfirmGoodBeen>() {
                    @Override
                    public void accept(ConfirmGoodBeen bean) throws Exception {
                        mView.onAddCarSuccess(bean,  position,  total_num);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }

    @Override
    public void selfOwnedShop(String limit) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.selfOwnedShop(limit).compose(RxScheduler.<SelfOwnedShopBeen>Flo_io_main())
                .as(mView.<SelfOwnedShopBeen>bindAutoDispose()).subscribe(new Consumer<SelfOwnedShopBeen>() {
            @Override
            public void accept(SelfOwnedShopBeen baseObjectBean) throws Exception {
                mView.hideLoading();
                mView.onSelfOwnedShopSuccess(baseObjectBean);

            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                mView.hideLoading();
                mView.onError(throwable);

            }
        });
    }

    //精选
    @Override
    public void initSelectData(boolean isRefresh, boolean isLoading, String pages, String limit) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
//        if (isLoading) {
//            mView.showLoading();
//        }
        model.initSelectData(pages, limit).compose(RxScheduler.<HomeMarketSelectBeen>Flo_io_main())
                .as(mView.<HomeMarketSelectBeen>bindAutoDispose()).subscribe(new Consumer<HomeMarketSelectBeen>() {
            @Override
            public void accept(HomeMarketSelectBeen baseObjectBean) throws Exception {
                mView.onInitSelectDataSuccess(baseObjectBean);
//                if (isLoading) {
//                    mView.hideLoading();
//                }
                if (isRefresh) {
                    mView.finishRefresh();
                } else {
                    mView.finishLoadMore();
                }
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                mView.onError(throwable);
//                if (isLoading) {
//                    mView.hideLoading();
//                }
                if (isRefresh) {
                    mView.finishRefresh();
                } else {
                    mView.finishLoadMore();
                }
            }
        });
    }



    @Override
    public void getGoodsLimitList(boolean isRefresh, boolean isLoading, String limit, int page) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        if (isLoading){
            mView.showLoading();
        }
        model.getGoodsLimitList(limit,page)
                .compose(RxScheduler.<GoodsLimitBean>Flo_io_main())
                .as(mView.<GoodsLimitBean>bindAutoDispose())
                .subscribe(new Consumer<GoodsLimitBean>() {
                    @Override
                    public void accept(GoodsLimitBean bean) throws Exception {
                        mView.getGoodsLimitListSuccess(bean);
                        if (isLoading){
                            mView.hideLoading();
                        }
                        if (isRefresh){
                            mView.finishRefresh();
                        }else {
                            mView.finishLoadMore();
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        if (isLoading){
                            mView.hideLoading();
                        }
                        if (isRefresh){
                            mView.finishRefresh();
                        }else {
                            mView.finishLoadMore();
                        }
                    }
                });

    }

    @Override
    public void getGoodsMiaoShaList(boolean isRefresh, boolean isLoading, String limit, int page) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        if (isLoading){
            mView.showLoading();
        }
        model.getGoodsMiaoShaList(limit,page)
                .compose(RxScheduler.<GoodsMiaoSBean>Flo_io_main())
                .as(mView.<GoodsMiaoSBean>bindAutoDispose())
                .subscribe(new Consumer<GoodsMiaoSBean>() {
                    @Override
                    public void accept(GoodsMiaoSBean bean) throws Exception {
                        mView.getGoodsMiaoShaListSuccess(bean);
                        if (isLoading){
                            mView.hideLoading();
                        }
                        if (isRefresh){
                            mView.finishRefresh();
                        }else {
                            mView.finishLoadMore();
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        if (isLoading){
                            mView.hideLoading();
                        }
                        if (isRefresh){
                            mView.finishRefresh();
                        }else {
                            mView.finishLoadMore();
                        }
                    }
                });
    }

}
